﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DSAServer.Models;
using DSAServer.Models.Input;
using Senparc.Weixin.Entities;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.QrCode;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Menu;

namespace DSAServer.Service
{
    public class WeixinService
    {
        public InvokeResult<AccessTokenResult> GetTicket(string appId, string appSecret)
        {
            InvokeResult<AccessTokenResult> result = new InvokeResult<AccessTokenResult>();

            if (!AccessTokenContainer.CheckRegistered(appId))//检查是否已经注册
            {
                AccessTokenContainer.Register(appId, appSecret);//如果没有注册则进行注册
            }
            result.data = AccessTokenContainer.GetAccessTokenResult(appId); //获取AccessToken结果
            if (result.data == null || string.IsNullOrEmpty(result.data.access_token))
            {
                throw new Exception("获取微信AccessToken授权失败");
            }
            return result;
        }

        public InvokeResult<CreateQrCodeResult> CreateQrCode(string appId, string appSecret, QrCodeParaInfo qcpi)
        {
            InvokeResult<CreateQrCodeResult> result = new InvokeResult<CreateQrCodeResult>();
            var accessToken = GetTicket(appId, appSecret);
            result.data = QrCodeApi.Create(accessToken.data.access_token, qcpi.expireSeconds, qcpi.sceneId);
            return result;
        }

        public InvokeResult<CreateQrCodeResult> CreateByStr(string appId, string appSecret, string sceneStr)
        {
            InvokeResult<CreateQrCodeResult> result = new InvokeResult<CreateQrCodeResult>();
            var accessToken = GetTicket(appId, appSecret);
            result.data = QrCodeApi.CreateByStr(accessToken.data.access_token, sceneStr);
            return result;
        }

        public InvokeResult<WxJsonResult> CreateMenu(string appId, string appSecret)
        {
            InvokeResult<WxJsonResult> result = new InvokeResult<WxJsonResult>();
            var rootmenu = WeixinMenuUtils.Instance;
            ButtonGroup bg = new ButtonGroup();
            foreach (WeixinMenu wm in rootmenu)
            {
                BaseButton button = null;
                if (wm.submenu != null && wm.submenu.Count > 0)
                {
                    button = new SubButton(wm.name);
                    foreach (WeixinMenu subwm in wm.submenu)
                    {
                        ((SubButton)button).sub_button.Add(
                            new SingleViewButton()
                            {
                                name = subwm.name,
                                url = subwm.url
                            });
                    }
                }
                else
                {
                    button = new SingleViewButton() { name = wm.name, url = wm.url };
                }
                bg.button.Add(button);
            }

            var accessToken = GetTicket(appId, appSecret);
            result.data = CommonApi.CreateMenu(accessToken.data.access_token, bg);
            result.ok = result.data != null;
            return result;
        }
    }
}